/*
题目链接：https://leetcode.cn/problems/magnetic-force-between-two-balls/description/
	刘沛民	2024-12-3
*/

class Solution {
public:
    int maxDistance(vector<int>& position, int m) {
        sort(position.begin(), position.end());

        auto check = [&](int x)->bool{
            int pre = -1e9;
            int cnt = 0;
            for(int i = 0; i < position.size(); i++)
            {
                if(position[i] - pre >= x) 
                {
                    cnt++;
                    pre = position[i];
                }
            }
            if(cnt >= m)    return true;
            return false;
        };

        int l = 1, r = 1e9;

        while(l < r)
        {
            int mid = l + r + 1>> 1;
            if(check(mid)) l = mid;
            else r = mid - 1;
        }
        return l;
    }
};